package com.hd.aop0410.controller;

import com.hd.aop0410.aop.CalculateTime;
import com.hd.aop0410.aop.NullValidate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@RestController
@RequestMapping("/test")
public class TestController {
    private Enumeration<String> headerNames;

    @GetMapping("/test1")
    @CalculateTime
    public String test1() throws InterruptedException {
        System.out.println("进入程序test1");
        Thread.sleep(10);
        return "test1";
    }
    @GetMapping("/test2")
    @CalculateTime(logfilepath = "D:/2204/log.txt",overtime = 10)
    public String test2() throws InterruptedException {
        System.out.println("进入程序test2");
        Thread.sleep(10);
        return "test2";
    }
    @GetMapping("/test3")
    @NullValidate
    public String test3(String name,String password){
        System.out.println(name+password);
        return "test3";
    }
    @PostMapping("/test4")
    public String test4(HttpServletRequest request,String name,String password){
        Enumeration<String> parameterNames = request.getParameterNames();
        if (parameterNames!=null) {
            while (parameterNames.hasMoreElements()) {
                System.out.println("请求参数：" + parameterNames.nextElement());
            }
        }else {
            System.out.println("没有请求参数");
        }

        //System.out.println("所有的请求头和请求体：");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String nextElement = headerNames.nextElement();//获取headerNames集合中的请求头
            String header2 = request.getHeader(nextElement);//通过请求头得到请求体
            System.err.println("请求头==key:" + nextElement);
            System.out.println( "请求体==value:" + header2);
        }
        //返回的是完整的url，包括Http协议，端口号，servlet名字和映射路径，但它不包含请求参数
        System.out.println("请求的url:"+request.getRequestURL());
        return "test4";
    }


}
